#include "LightingRenderPass.h"
#include <SERender/Renderer/Renderer.h>
#include <SEDRPPlugin/GBuffer.h>
#include <SERender/Descriptors/FrameDescriptor.h>
#include <SEngine/Render/RenderFeature/UtilRenderFeature.h>
#include <SEUi/SEUi.h>
#include <SERender/Resourses/Loader/ShaderLoader.h>
#include <SEngine/Render/RenderFeature/UBORenderFeature.h>

namespace SEngine
{
    LightRenderPass::LightRenderPass(Renderer &renderer)
        : RenderPass(renderer)
    {
        m_defPBRMaterial = Manager::GetInstance().Get<Material>("DeferPBR").value();
        m_defPBRMaterial.Value().GetPSO().blend = true;
        m_defPBRMaterial.Value().GetPSO().depth = true;
    }

    void LightRenderPass::DrawFrame()
    {
        auto & gbuffer = m_renderer.AnyDescriptor::GetDescriptor<GBuffer>()->get();
        auto & frame = m_renderer.AnyDescriptor::GetDescriptor<FrameDescriptor>()->get();
        auto & uitlFeature = m_renderer.Descriptor<RenderFeature>::GetDescriptor<UtilRenderFeature>()->get();
        m_defPBRMaterial.Value().Set("PositionMetalRT", Shader::Sampler{*gbuffer["PositionMetalRT"]});
        m_defPBRMaterial.Value().Set("AlbedoRoughRT", Shader::Sampler{*gbuffer["AlbedoRoughRT"]});
        m_defPBRMaterial.Value().Set("NormalDepthRT", Shader::Sampler{*gbuffer["NormalDepthRT"]});
        uitlFeature.DrawBillboard(m_defPBRMaterial);
        
        SignalUpdateUI.AddSlot([this]() {
            ImGui::PushID("LightRenderPass");
            if (ImGui::CollapsingHeader("LightRenderPass")) {
                if (ImGui::Button("Reload")) {
                    m_defPBRMaterial.Value().GetShader().Reload([](auto & path, auto & name) {
                        ShaderLoader loader;
                        return loader.Load(path).CreateShader(EngineUniformBlockBinder());
                    });
                }
            }
            ImGui::PopID();
            return SlotState::Once;
        });
    }
}